package cn.kgc.eduservice.controller;


import cn.kgc.commonutils.R;
import cn.kgc.eduservice.entity.EduTeacher;
import cn.kgc.eduservice.entity.vo.TeacherQuery;
import cn.kgc.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-22
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin   //解决跨域问题     : 协议 、 IP地址  、端口
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;
    //查询
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R allTeacher(){

        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);

    }
    //逻辑删除讲师的方法
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){

        boolean flag = teacherService.removeById(id);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @ApiOperation(value = "分页讲师列表")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher (@ApiParam(name = "current", value = "当前页数", required = true)@PathVariable Long current,
                              @ApiParam(name = "limit", value = "每页记录数", required = true)@PathVariable Long limit){
        // 创建page对象
        Page<EduTeacher> teacherPage = new Page<>(current,limit);
        // 调用 方法来实现分页
         teacherService.page(teacherPage, null);

         Long total = teacherPage.getTotal(); //总记录数
        List<EduTeacher> records = teacherPage.getRecords();// 数据list 集合

//        Map map = new HashMap();
//        map.put("totals",total);
//        map.put("rows",records);
//        return R.ok().data(map);
        return R.ok().data("totals",total).data("rows",records);
    }
    // 条件查询带分页的方法
    @ApiOperation(value = "分页条件查询讲师列表")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@ApiParam(name = "current", value = "当前页数", required = true)@PathVariable Long current,
                                  @ApiParam(name = "limit", value = "每页记录数", required = true)@PathVariable Long limit,
                                  @RequestBody(required = false)TeacherQuery teacherQuery){
        // 创建page对象
        Page<EduTeacher> teacherPage = new Page<>(current,limit);
        //构件条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String ent = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(ent)){
            wrapper.le("gmt_create",ent);
        }
        wrapper.orderByDesc("gmt_create");
        teacherService.page(teacherPage, wrapper);

        Long total = teacherPage.getTotal(); //总记录数
        List<EduTeacher> records = teacherPage.getRecords();// 数据list 集合
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "添加讲师列表")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @ApiOperation(value = "根据讲师ID查询")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){
        EduTeacher teacherById = teacherService.getById(id);
        return  R.ok().data("teacher",teacherById);
    }

    @ApiOperation(value = "讲师修改功能")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = teacherService.updateById(eduTeacher);
        if(b){
            return R.ok();
        }else {
            return R.error();
        }
    }

}

